package com.gitee.wsl.flow.create.datasource.source

import com.gitee.wsl.ext.base.currentTimeMillis
import com.gitee.wsl.flow.create.datasource.FlowDataSource
import com.gitee.wsl.flow.create.datasource.request.ExpiringExecutableRequest
import kotlin.coroutines.CoroutineContext
import kotlin.coroutines.EmptyCoroutineContext

open class ExpiringExecutableFlowDataSource<T>(
    cacheDataSource: FlowDataSource<ExpiringExecutableRequest<T>, FlowDataSourceExpiringValue<T>>? = null,
    coroutineContext: CoroutineContext = EmptyCoroutineContext
) : BaseExpiringExecutableFlowDataSource<ExpiringExecutableRequest<T>, T>(cacheDataSource, coroutineContext) {

    override suspend fun internalRead(request: ExpiringExecutableRequest<T>) =
        FlowDataSourceExpiringValue(request.block(), currentTimeMillis + request.expiredInMilliseconds)
}
